<!DOCTYPE html>
<title>EventSet Tests</title>
<script src="../src/ObjectContainer.js"></script>
<script src="../src/EventSet.js"></script>
<script>    
    function assert(expr, message) {        
        if (!expr) {
            document.write('<span style="color: red">' + message + '</span><br/>');
            throw new Error(message);
        }
    }
    
    var es = new EventSet(),        
        test_passed_1 = false,
        test_passed_2 = false,
        test_passed_3 = false,
        test_fire_passed = false;

    function first() { test_passed_1 = true; return 'first'; }
    function second() { test_passed_2 = true; return 'second'; }
    
    es.add('test');    
    es.add('test', first);    
    es.test(second);    
    var result = es.fire('test');    
    assert(test_passed_1 && test_passed_2, "EventSet.add(...) test failed!");            
    es.add('test_new', function () { test_passed_3 = true; });    
    es.fire('test_new');    
    assert(test_passed_3, "EventSet.add(...) test failed!");   
    document.write("EventSet.add(...) test passed.<br/>");
    
    assert(result[0] === 'first' && result[1] === 'second', "EventSet.fire(...) test failed!");    
    document.write("EventSet.fire(...) test passed.<br/>");
        
    var banned = ['add', 'remove', 'events'];
    for (var id in banned) {
        try {        
            es.add(banned[id], function () {});
        } catch(e) {            
            assert(e.name === 'EventSet', "EventSet.add(...) reserved names error test failed!");
        }
        
        assert(es.events.indexOf(banned[id]) === -1, "EventSet.add(...) reserved names error test failed!");
    }
    document.write("EventSet.add(...) reserved names error test passed.<br/>");
    
    es.remove(second);
    test_passed_2 = false;
    es.fire('test');
    assert(!test_passed_2, "EventSet.remove(handler) test failed!");
    document.write("EventSet.remove(handler) test passed.<br/>");
    
    es.remove('test_new');
    assert(!es['test_new'], "EventSet.remove(event) test failed!");
    assert(es.events.indexOf('test_new') === -1, "EventSet.remove(event) test failed!");
    document.write("EventSet.remove(event) test passed.<br/>");
    
    // TODO: Test for .nameOf(...)
    // TODO: Test for .add(Array) and .add(Object)
</script>